﻿using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyTuyenSinh.Base;

namespace QuanLyTuyenSinh.PresentationLayer.Components
{
    public partial class frmNhapdiem : Form
    {
        //private frmDangnhap objDN;
        //MonthiBUS monthibus = new MonthiBUS();
        //ThisinhBUS thisinhbus = new ThisinhBUS();
        private SqlConnection Conn;
        public frmNhapdiem()
        {
            InitializeComponent();
        }

        private void frmNhapdiems_Load(object sender, EventArgs e)
        {
            //Conn = ConnectData.Getcon();
            //Conn.Open();
            //comboKhoithi.DataSource = monthibus.GetKhoithiUngVoiNganhHoc();
            //comboKhoithi.DisplayMember = "Khoithi";
            //comboKhoithi.ValueMember = "Khoithi";
            //checkboxMon1.Checked = false;
            //checkboxMon2.Checked = false;
            //checkboxMon3.Checked = false;
            //cmdNhapdiem.Enabled = false;
            //comboSTTTui.Enabled = false;
        }

        private void comboKhoithi_SelectedIndexChanged(object sender, EventArgs e)
        {
            checkboxMon1.Checked = false;
            checkboxMon2.Checked = false;
            checkboxMon3.Checked = false;
        }

        private void checkboxMon1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkboxMon1.Checked == true)
            {
                cmdNhapdiem.Enabled = true;
                comboSTTTui.Enabled = true;
               // comboSTTTui.DataSource = thisinhbus.GetTuiFromPhach(comboKhoithi.Text.Trim(), "M1");
                comboSTTTui.DisplayMember = "TuiM1";
                comboSTTTui.ValueMember = "TuiM1";
                DataGridNhapdiem.Columns[0].ReadOnly = true;
                DataGridNhapdiem.Columns[1].ReadOnly = true;
                DataGridNhapdiem.Columns[2].ReadOnly = true;
            }
        }

        private void comboSTTTui_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkboxMon1.Checked == true)
                {
                   // DataGridNhapdiem.DataSource = thisinhbus.GetPhachDeNhapDiem(comboKhoithi.Text.Trim(), "M1", comboSTTTui.Text.Trim());
                    DataGridNhapdiem.Columns[0].HeaderText = "STT";
                    DataGridNhapdiem.Columns[1].HeaderText = "Khối";
                    DataGridNhapdiem.Columns[2].HeaderText = "Phách M1";
                    DataGridNhapdiem.Columns[3].HeaderText = "Điểm M1";
                }
                else
                    if (checkboxMon2.Checked == true)
                    {
                       // DataGridNhapdiem.DataSource = thisinhbus.GetPhachDeNhapDiem(comboKhoithi.Text.Trim(), "M2", comboSTTTui.Text.Trim());
                        DataGridNhapdiem.Columns[0].HeaderText = "STT";
                        DataGridNhapdiem.Columns[1].HeaderText = "Khối";
                        DataGridNhapdiem.Columns[2].HeaderText = "Phách M2";
                        DataGridNhapdiem.Columns[3].HeaderText = "Điểm M2";
                    }
                    else
                        if (checkboxMon3.Checked == true)
                        {
                            //DataGridNhapdiem.DataSource = thisinhbus.GetPhachDeNhapDiem(comboKhoithi.Text.Trim(), "M3", comboSTTTui.Text.Trim());
                            DataGridNhapdiem.Columns[0].HeaderText = "STT";
                            DataGridNhapdiem.Columns[1].HeaderText = "Khối";
                            DataGridNhapdiem.Columns[2].HeaderText = "Phách M3";
                            DataGridNhapdiem.Columns[3].HeaderText = "Điểm M3";
                        }
                        else return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void checkboxMon2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkboxMon2.Checked == true)
            {
                cmdNhapdiem.Enabled = true;
                comboSTTTui.Enabled = true;
               // comboSTTTui.DataSource = thisinhbus.GetTuiFromPhach(comboKhoithi.Text.Trim(), "M2");
                comboSTTTui.DisplayMember = "TuiM2";
                comboSTTTui.ValueMember = "TuiM2";
                DataGridNhapdiem.Columns[0].ReadOnly = true;
                DataGridNhapdiem.Columns[1].ReadOnly = true;
                DataGridNhapdiem.Columns[2].ReadOnly = true;
            }
        }

        private void checkboxMon3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkboxMon3.Checked == true)
            {
                cmdNhapdiem.Enabled = true;
                comboSTTTui.Enabled = true;
             //   comboSTTTui.DataSource = thisinhbus.GetTuiFromPhach(comboKhoithi.Text.Trim(), "M3");
                comboSTTTui.DisplayMember = "TuiM3";
                comboSTTTui.ValueMember = "TuiM3";
                DataGridNhapdiem.Columns[0].ReadOnly = true;
                DataGridNhapdiem.Columns[1].ReadOnly = true;
                DataGridNhapdiem.Columns[2].ReadOnly = true;
            }
        }

        private void cmdNhapdiem_Click(object sender, EventArgs e)
        {
            try
            {
                string sql;
                if (checkboxMon1.Checked == true)
                {
                    for (int i = 0; i < DataGridNhapdiem.Rows.Count - 1; i++)
                    {
                        DataGridViewRow row = DataGridNhapdiem.Rows[i];
                        string Phachx = row.Cells[2].Value.ToString();
                        float diem = float.Parse(row.Cells[3].Value.ToString());
                        sql = "update TSSBD set DiemM1=@Diem where PhachM1=@Phach and Khoithi='" + comboKhoithi.Text + "'";
                        SqlCommand cmd = new SqlCommand(sql, Conn);
                        cmd.Parameters.Add(new SqlParameter("Diem", diem));
                        cmd.Parameters.Add(new SqlParameter("Phach", Phachx));
                        cmd.ExecuteNonQuery();
                    }
                    MessageBox.Show("Xong!");
                }
                if (checkboxMon2.Checked == true)
                {
                    for (int i = 0; i < DataGridNhapdiem.Rows.Count - 1; i++)
                    {
                        DataGridViewRow row = DataGridNhapdiem.Rows[i];
                        string Phachx = row.Cells[2].Value.ToString();
                        float diem = float.Parse(row.Cells[3].Value.ToString());
                        sql = "update TSSBD set DiemM2='" + diem + "' where PhachM2='" + Phachx.ToString().Trim() + "' and Khoithi='" + comboKhoithi.Text + "'";
                        SqlCommand cmd = new SqlCommand(sql, Conn);
                        cmd.ExecuteNonQuery();
                    }
                    MessageBox.Show("Xong!");
                }
                if (checkboxMon3.Checked == true)
                {
                    for (int i = 0; i < DataGridNhapdiem.Rows.Count - 1; i++)
                    {
                        DataGridViewRow row = DataGridNhapdiem.Rows[i];
                        string Phachx = row.Cells[2].Value.ToString();
                        float diem = float.Parse(row.Cells[3].Value.ToString());
                        sql = "update TSSBD set DiemM3='" + diem + "' where PhachM3='" + Phachx.ToString().Trim() + "' and Khoithi='" + comboKhoithi.Text + "'";
                        SqlCommand cmd = new SqlCommand(sql, Conn);
                        cmd.ExecuteNonQuery();
                    }
                    MessageBox.Show("Xong!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

      }
}
